// miniprogram/pages/test/answer/answer.js
const answer_db = wx.cloud.database().collection("testAnswer")
var time_utils = require('../../../utils/time_utils');

Page({

  data: {
    flag: '',
    answerData: {}
  },
  onLoad: function (options) {
    var publishId = options.publishId
    var flag = options.flag
    console.log(publishId)
    answer_db.doc(publishId).get().then(res => {
      console.log(res)
      this.setData({
        answerData: res.data,
        flag: flag
      })
    })
  },
  onShareAppMessage: function () {

  },
  //上传分数
  submitScore(e) {
    console.log(e)
    var dateTime = time_utils.getDateTime()
    var answerData = this.data.answerData
    //修改状态及分数数据
    answer_db.doc(answerData._id).update({
        data: {
          testStatus: "已判分",
          scoreTime: dateTime,
          testData: answerData.testData
        },
      }).then(res => {
        console.log("判分成功")
        wx.showToast({
          title: '上传分数成功',
          icon: "none"
        })
        wx.navigateBack({
          complete: (res) => {},
        })
      })
      .catch(console.error)
  },
  //输入分数监听
  inputScoreListener(e) {
    console.log(e)
    var score = e.detail.value
    var index = e.currentTarget.dataset.index
    var answerData = this.data.answerData
    var scoreValue = answerData.testData.TestList[index].scoreValue
    if (parseInt(score) > parseInt(scoreValue.replace("分", ""))) {
      wx.showToast({
        title: '不能大于题目分数',
        icon: 'none'
      })
      return
    }
    //赋值scoreAdmin
    answerData.testData.TestList[index].scoreAdmin = score
    //计算总成绩
    var AdminScore = 0;
    for (let i = 0; i < answerData.testData.TestList.length; i++) {
      var scoreAdmin = answerData.testData.TestList[index].scoreAdmin
      console.log("scoreAdmin", scoreAdmin)
      AdminScore = parseInt(AdminScore) + parseInt(scoreAdmin)
    }
    answerData.testData.AdminScore = AdminScore
    this.setData({
      answerData: answerData
    })
  }
})